// 迭代器
// JQuery
$.each([1,2,3],(idx,i)=>{
  console.log(idx,'下标')
  console.log(i,'值')
})

// 基本迭代器
function each(arr,callback){
  for(var i = 0;i<arr.length;i++){
    callback.call(arr[i],i,arr[i])
  }
}

// 内部迭代器
// 不修改内部迭代规则
const compare = function(arr1,arr2){
  if(arr1.length!==arr2.length){
    throw  new Error('不相等')
  }
  each(arr1,function(i,n){
    if(n!==arr2[i]){
      throw new Error('不相等')
    }
  })
  console.log('相等')
}

// 编写迭代器
const Iterator = function(obj){
  let current = 0
  let next = function(){
    current = current + 1
  }
  let isDone = function(){
    return current>= obj.length
  }
  let getCurrentItem = function(){
    return obj[current]
  }
  return {
    next,
    isDone,
    getCurrentItem,
    length: obj.length
  }
}

// 外部迭代器
const compare = function(iterator1,iterator2){
  if(iterator1.length!==iterator2.length){
    throw new Error('不相等')
  }
  while(!iterator1.isDone()&&!iterator2.isDone()){
    if(iterator1.getCurrentItem()!==iterator2.getCurrentItem()){
      throw new Error('不相等')
    }
    iterator1.next()
    iterator2.next()
  }
  alert('相等')
}
const iterator1 = Iterator([1,2,3,4])
const iterator2 = Iterator([1,2,3,4])

compare(iterator1,iterator2)

// 迭代器类数组对象/字面量对象
// arguments {'0':a,'1':2,length: 1}
$.each = function(obj,callback){
  let value
  let i = 0
  let length = obj.length
  let isArray = isArrayLike(obj)
  if(isArray){
    for(;i<length;i++){
      value =  callback.call(obj[i],i,obj[i])
      if(value === false){
        break
      }
    } 
  }else{
    for( i in obj ){
      value =  callback.call(obj[i],i,obj[i])
      if(value === false){
        break
      }
    }
  }
}

function isArrayLike(obj){
}

// 倒叙迭代器
function reverseEach(arr,callback){
  for(var i = arr.length;i>0;i--){
    callback.call(arr[i],i,arr[i])
  }
}
// 终止迭代器
function each(arr,callback){
  for(var i = 0;i<arr.length;i++){
    if(callback.call(arr[i],i,arr[i])===false){
      break
    }
  }
}

// 上传
const getUploadComponent = function(){
  try{
    return new ActiveXObject('TXFINActiveX.FYUpload') //IE
  }catch{
    if(supportFlash()){
      return '<object type="application/flash"><object>'
    }else{
      return '<input type="file" name="file" />'
    }
  }
}
const getActiveXObject = function(){
  try{
    return new ActiveXObject('TXFINActiveX.FYUpload') //IE
  }catch(e){
    return false
  }
}
const getFlash = function(){
  if(supportFlash()){
    return '<object type="application/flash"><object>'
  }
  return false
}
const getFormUplad(){
  return '<input type="file" name="file" />'
}

const getHTMLUpload=function(){
  // ...
}

const IteratorUploadobj = function(){
  let fn
  for(let i = 0;fn =arguments[i];i++){
    let uploadobj = fn()
    if(uploadobj){
      return uploadobj
    }
  }
}

const uploadboj = IteratorUploadobj(getActiveXObject,getFlash,IteratorUploadobj,getHTMLUpload)

// uploadboj

// 发布订阅
// 一对多
document.body.addEventListener('click',()=>{
  console.log('click')
})
document.body.addEventListener('click',()=>{
  console.log('click2')
})

document.body.addEventListener('click',()=>{
  console.log('click3')
})

document.body.addEventListener('click',()=>{
  console.log('click4')
})
document.body.click()

// 手写发布订阅
const saleOffices = {
  '80'[()=.{},()=>{}]
} //发布者
saleOffices.clientList = {} //缓存列表 存放订阅者的回调函数'
saleOffices.lesten = (key,fn)=>{
  if(!this.clientList[key]){
    this.clientList[key] = []
  }
  this.clientList[key].push(fn)
}
saleOffices.trigger = ()=>{
  let key = Array.prototype.shift.call(arguments) //去消息类型
  let fns = this.clientList[key]
  if(!fns||fns.length ===0){
    return
  }
  for(let i=0;i<fns.length;i++){
    let fn = fns[i]
    fn.apply(this,arguments)
  }
}

saleOffices.lesten('80',(time)=>{
  console.log(80+time)
})
saleOffices.lesten('80',(time)=>{
  console.log(80+'111')
})
saleOffices.lesten('100',(time)=>{
  console.log(100+time)
})
saleOffices.trigger('80',2000)
saleOffices.trigger('100',3000)


const event = {
  clientList: {}
  lesten:function(key,fn){
    if(!this.clientList[key]){
      this.clientList[key] = []
    }
    this.clientList[key].push(fn)
  },
  trigger: function(){
    let key = Array.prototype.shift.call(arguments) //去消息类型
    let fns = this.clientList[key]
    if(!fns||fns.length ===0){
      return
    }
    for(let i=0;i<fns.length;i++){
      let fn = fns[i]
      fn.apply(this,arguments)
    }
  },
  remove:function(key,fn){
    let fns = this.clientList[key]
    if(!fns){
      return
    }
    if(!fn){
      fns&&fns.length=0
    }else{
      for(let i = fns.length;i>=0,i++){
        let _fn = fns[i]
        if(_fn===fn){
          fns.slice(i,1)
        }
      }
    }
  }
}

const installEvent = function(obj){
  for(let i in event){
    obj[i] = event[i]
  }
}

const saleOffices1 = {

}

installEvent(saleOffices1)

// 全局中介
const Event1 = (function(){
  let clientList = {}
  let lesten
  let trigger
  let remove
  lesten = function(key,fn){
    if(!clientList[key]){
      clientList[key] = []
    }
    clientList[key].push(fn)
  }
  trigger = function(){
    let key = Array.prototype.shift.call(arguments) //去消息类型
    let fns = clientList[key]
    if(!fns||fns.length ===0){
      return
    }
    for(let i=0;i<fns.length;i++){
      let fn = fns[i]
      fn.apply(this,arguments)
    }
  }
  remove = function(key,fn){
    let fns = clientList[key]
    if(!fns){
      return
    }
    if(!fn){
      fns&&fns.length=0
    }else{
      for(let i = fns.length;i>=0,i++){
        let _fn = fns[i]
        if(_fn===fn){
          fns.slice(i,1)
        }
      }
    }
  }
  return {
    clientList,
    lesten,
    trigger,
    remove,
  }
})()

const f80= (time)=>{
  console.log(time)
}

Event1.lesten('80',f80)
Event1.lesten('100',(time)=>{
  console.log(time)
})
Event1.lesten('80',(time)=>{
  console.log(time)
})
Event1.trigger('80', 5000)
Event1.remove('80',f80)
